﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using WMSADL.SY;
using WMSAML.SY;

namespace WMSABL.SY
{
    public class TOrganization_BLL
    {
        PublicDataSet bll_comm = new PublicDataSet();
        TOrganization_DAL dal = new TOrganization_DAL();
        public int Add(TOrganization_Entity entity)
        {
            int resultAdd = 0;
            TOrganization_Entity entity_TOrganization = new TOrganization_Entity();
            int result = dal.Add(entity);
            if (result > 0)
            {
                entity_TOrganization = GetEntityByID(result);
                if (entity_TOrganization != null)
                {
                    int parentid = entity_TOrganization.Parent_id;
                    string family = "";
                    if (bll_comm.GetDataSet("TOrganization", " Parent_id=" + parentid + " ").Tables[0].Rows.Count > 1)
                    {
                        family = (Convert.ToInt32(GetEntityByParent_id(entity.Parent_id).Family) + 1).ToString();
                    }
                    else
                    {
                        family = GetEntityByID(entity.Parent_id).Family + "01";
                    }
                    entity_TOrganization.Family = family;
                    if (Update(entity_TOrganization))
                    {
                        resultAdd = entity_TOrganization.Organization_id;
                    }
                }
            }

            return resultAdd;
        }

        public bool Delete(int id)
        {
            return dal.Delete(id);
        }
        public bool Update(TOrganization_Entity entity)
        {
            return dal.Update(entity);
        }
        public bool ExistsByID(int id)
        {
            return dal.ExistsByID(id);
        }
        public bool ExistsByPID(int pid)
        {
            return dal.ExistsByPID(pid);
        }
        public bool ExistsByName(int organization_name)
        {
            return dal.ExistsByName(organization_name);
        }
        public TOrganization_Entity GetEntityByName(string organization_name)
        {
            return dal.GetEntityByName(organization_name);
        }
        public TOrganization_Entity GetEntityByID(int id)
        {
            return dal.GetEntityByID(id);
        }
        public V_TOrganization_Entity GetEntityByIDV(int id)
        {
            return dal.GetEntityByIDV(id);
        }
        public TOrganization_Entity GetEntityByParent_id(int parent_id)
        {
            return dal.GetEntityByParent_id(parent_id);
        }
        public DataSet GetList_TOrganization()
        {
            return dal.GetList_TOrganization();
        }
    }
}
